package com.ruoyi.system.service.honor.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.honor.studentaward.StudentAwardInfo;
import com.ruoyi.system.domain.honor.studentaward.StudentAwardMember;
import com.ruoyi.system.mapper.honor.StudentAwardInfoMapper;
import com.ruoyi.system.mapper.honor.StudentAwardMemberMapper;
import com.ruoyi.system.service.honor.IStudentAwardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 指导学生竞赛Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class StudentAwardInfoServiceImpl implements IStudentAwardInfoService
{
    @Autowired
    private StudentAwardInfoMapper studentAwardInfoMapper;
    @Autowired
    private StudentAwardMemberMapper studentAwardMemberMapper;
    /**
     * 查询指导学生竞赛
     * 
     * @param id 指导学生竞赛主键
     * @return 指导学生竞赛
     */
    @Override
    public StudentAwardInfo selectStudentAwardInfoById(Long id)
    {
        StudentAwardInfo ra = studentAwardInfoMapper.selectStudentAwardInfoById(id);
        StudentAwardMember studentAwardMember = new StudentAwardMember();
        studentAwardMember.setClassId(id);
        List<StudentAwardMember> studentAwardMembers = studentAwardMemberMapper.selectStudentAwardMemberList(studentAwardMember);
        ra.setStudentAwardMembers(studentAwardMembers);

        return ra;
    }

    /**
     * 查询指导学生竞赛列表
     * 
     * @param studentAwardInfo 指导学生竞赛
     * @return 指导学生竞赛
     */
    @Override
    public List<StudentAwardInfo> selectStudentAwardInfoList(StudentAwardInfo studentAwardInfo)
    {
        //如果是老师，则只显示自己上传的
        if(studentAwardInfo.getCurrentRole()!=null&&studentAwardInfo.getCurrentRole().equals("teacher")){
            studentAwardInfo.setTeacherId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(studentAwardInfo.getCurrentRole()!=null&&studentAwardInfo.getCurrentRole().equals("department")){
            studentAwardInfo.setDeptId(SecurityUtils.getDeptId());
        }
        return studentAwardInfoMapper.selectStudentAwardInfoList(studentAwardInfo);
    }

    /**
     * 新增指导学生竞赛
     * 
     * @param studentAwardInfo 指导学生竞赛
     * @return 结果
     */
    @Override
    public int insertStudentAwardInfo(StudentAwardInfo studentAwardInfo)
    {
        if(studentAwardInfo.getId()!=null){
            StudentAwardMember studentAwardMember = new StudentAwardMember();
            studentAwardMember.setClassId(studentAwardInfo.getId());
            studentAwardMember.setStudentName(studentAwardInfo.getStudentName());
            studentAwardMember.setStudentType(studentAwardInfo.getStudentType());
            studentAwardMemberMapper.insertStudentAwardMember(studentAwardMember);
        }else {
            int result = studentAwardInfoMapper.insertStudentAwardInfo(studentAwardInfo);
            if(result>0){
                StudentAwardMember studentAwardMember = new StudentAwardMember();
                studentAwardMember.setClassId(studentAwardInfo.getId());
                studentAwardMember.setStudentName(studentAwardInfo.getStudentName());
                studentAwardMember.setStudentType(studentAwardInfo.getStudentType());
                studentAwardMemberMapper.insertStudentAwardMember(studentAwardMember);
            }
        }
        return 1;
    }

    /**
     * 修改指导学生竞赛
     * 
     * @param studentAwardInfo 指导学生竞赛
     * @return 结果
     */
    @Override
    public int updateStudentAwardInfo(StudentAwardInfo studentAwardInfo)
    {
        return studentAwardInfoMapper.updateStudentAwardInfo(studentAwardInfo);
    }

    /**
     * 批量删除指导学生竞赛
     * 
     * @param ids 需要删除的指导学生竞赛主键
     * @return 结果
     */
    @Override
    public int deleteStudentAwardInfoByIds(Long[] ids)
    {
        for(Long id:ids){
            StudentAwardMember studentAwardMember = new StudentAwardMember();
            studentAwardMember.setClassId(id);
            List<StudentAwardMember> studentAwardMembers = studentAwardMemberMapper.selectStudentAwardMemberList(studentAwardMember);
            studentAwardMemberMapper.deleteStudentAwardMemberByIds(studentAwardMembers.stream().map(StudentAwardMember::getId).toArray(Long[]::new));
        }
        return studentAwardInfoMapper.deleteStudentAwardInfoByIds(ids);
    }

    /**
     * 删除指导学生竞赛信息
     * 
     * @param id 指导学生竞赛主键
     * @return 结果
     */
    @Override
    public int deleteStudentAwardInfoById(Long id)
    {
        return studentAwardInfoMapper.deleteStudentAwardInfoById(id);
    }
    /**
     * 根据标题查询
     * @param data
     * @return
     */
    @Override
    public StudentAwardInfo getPaperInfoByTitle(String data) {
        return studentAwardInfoMapper.getPaperInfoByTitle(data);
    }
}
